package com.zmn.oms.dubbo.impl.zmn.third.yunding;

import org.apache.dubbo.config.annotation.Service;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.dto.zmn.third.yunding.YundingOldPartDIO;
import com.zmn.oms.dubbo.dto.zmn.third.yunding.YundingOrderVO;
import com.zmn.oms.dubbo.dto.zmn.third.yunding.YundingSparePartVO;
import com.zmn.oms.dubbo.interfaces.zmn.third.yunding.YundingRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.third.yunding.YundingConsts;
import com.zmn.oms.third.yunding.api.YundingOrderCacheData;
import com.zmn.oms.third.yunding.dto.YundingOldPartSendbackDTO;
import com.zmn.oms.third.yunding.service.YundingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * @author xiewenbing
 * @date:2020/09/14 3:58 下午
 * description: 云丁H5 Dubbo接口
 */
@Slf4j
@Api(value = "云丁H5接口", tags = {"云丁H5接口"})
@Service(loadbalance = "roundrobin", timeout = 5000, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class YundingRemoteServiceImpl implements YundingRemoteService {

    @Autowired
    private YundingService yundingServcie;

    /**
     * 获取云丁渠道订单的相关信息以及保存的填写资料
     *
     * @param orderId
     * @return
     */
    @Override
    @ApiOperation(value = "查询云丁订单的资料")
    public ResponseDTO<YundingOrderVO> getYundingOrderData(Long orderId) {

        // 调用接口读取资料
        YundingOrderCacheData yundingOrderCacheData = null;
        try {
            yundingOrderCacheData = yundingServcie.getOrderShowData(orderId);
        } catch (OmsBaseException exception) {
            return ResponseDTO.fail(exception.getMessage());
        }

        // 数据转换
        YundingOrderVO yundingOrderVO = BeanMapper.map(yundingOrderCacheData,YundingOrderVO.class);
        yundingOrderVO.setOrderId(orderId);
        yundingOrderVO.setYdOrderNumber(yundingOrderCacheData.getOrderNumber());
        yundingOrderVO.setExpressCompany(yundingOrderCacheData.getExpressCompanyName());
        yundingOrderVO.setExpressPicture(yundingOrderCacheData.getExpressPackagesPicturesUrl());

        if (Objects.nonNull(yundingOrderCacheData.getUrgentMaintain())) {
            yundingOrderVO.setUrgent(yundingOrderCacheData.getUrgentMaintain() ? GlobalConsts.YES : GlobalConsts.NO);
        } else {
            yundingOrderVO.setUrgent(GlobalConsts.NO);
        }

        if (Objects.nonNull(yundingOrderCacheData.getOrderInstall()) && !yundingOrderCacheData.getOrderInstall()) {
            if (Objects.nonNull(yundingOrderCacheData.getOldPartSendBack())) {
                yundingOrderVO.setSendBack(yundingOrderCacheData.getOldPartSendBack() ? GlobalConsts.YES : GlobalConsts.NO);
            } else {
                yundingOrderVO.setSendBack(GlobalConsts.NO);
            }

            if (StringUtil.isNotBlank(yundingOrderCacheData.getRecommendProcessMethodCode())) {
                yundingOrderVO.setRecommendProcessMethod(YundingConsts.getProcessMethodIndex(yundingOrderCacheData.getRecommendProcessMethodCode()));
            }

            if (StringUtil.isNotBlank(yundingOrderCacheData.getProcessMethodCode())) {
                yundingOrderVO.setProcessMethod(YundingConsts.getProcessMethodIndex(yundingOrderCacheData.getProcessMethodCode()));
            }

            if (CollectionUtil.isNotNullOrEmpty(yundingOrderCacheData.getSparePartsSendBackList())) {
                List<YundingSparePartVO> sparePartVOList = BeanMapper.mapList(yundingOrderCacheData.getSparePartsSendBackList(),
                        YundingSparePartVO.class);
                yundingOrderVO.setSparePartList(sparePartVOList);
            }

            yundingOrderVO.setInstallOrder(GlobalConsts.NO);
        } else {
            yundingOrderVO.setInstallOrder(GlobalConsts.YES);
        }

        log.info("#YundingRemoteService#getYundingOrderData result:[{}]",yundingOrderVO.toString());
        
        return ResponseDTO.success(yundingOrderVO);
    }

    /**
     * 保存云丁渠道订单填写的相关资料
     *
     * @param yundingOrderVO
     * @return
     */
    @Override
    @ApiOperation(value = "保存云丁订单的填写资料")
    public ResponseDTO saveYundingOrderData(YundingOrderVO yundingOrderVO) {

        if (Objects.isNull(yundingOrderVO)) {
            return ResponseDTO.fail("参数不能为null");
        }

        YundingOrderCacheData yundingOrderCacheData = BeanMapper.map(yundingOrderVO,YundingOrderCacheData.class);
        yundingOrderCacheData.setOrderInstall(Objects.equals(yundingOrderVO.getInstallOrder(),GlobalConsts.NO) ? Boolean.FALSE : Boolean.TRUE);

        if (Objects.nonNull(yundingOrderVO.getProcessMethod())) {
            yundingOrderCacheData.setProcessMethodCode(YundingConsts.getProcessMethodByIndex(yundingOrderVO.getProcessMethod()));
            yundingOrderCacheData.setProcessMethodName(YundingConsts.getProcessMethodName(yundingOrderCacheData.getProcessMethodCode()));
        }

        try {
            yundingServcie.saveOrderShowData(yundingOrderVO.getOrderId(),yundingOrderCacheData);
        } catch (OmsBaseException exception) {
            return ResponseDTO.fail(exception.getMessage());
        }
        return ResponseDTO.success();
    }

    /**
     * 同步云丁渠道的旧件回寄信息
     *
     * @param yundingOldPartDIO
     * @return
     */
    @Override
    @ApiOperation(value = "同步云丁订单的旧件回寄")
    public ResponseDTO syncOldPartSendBack(YundingOldPartDIO yundingOldPartDIO) {

        YundingOldPartSendbackDTO oldPartSendbackDTO = new YundingOldPartSendbackDTO();
        oldPartSendbackDTO.setOrderNumber(String.valueOf(yundingOldPartDIO.getOrderId()));
        oldPartSendbackDTO.setExpressCompanyName(yundingOldPartDIO.getExpressCompany());
        oldPartSendbackDTO.setExpressNumber(yundingOldPartDIO.getExpressNumber());
        oldPartSendbackDTO.setExpressPackagesPicturesUrl(yundingOldPartDIO.getExpressPicture());

        try {
            yundingServcie.syncOldPartSendBack(oldPartSendbackDTO);

            return ResponseDTO.success();
        } catch (OmsBaseException exception) {

            return ResponseDTO.fail(exception.getMessage());
        }
    }
}
